package com.xwj.fallback;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.fastjson.JSON;
import com.xwj.dto.CommonResult;
import com.xwj.service.PaymentService;

/**
 * @author xwj
 * @date 2020/10/28
 */
public class PaymentServiceFallback implements PaymentService {
    private Throwable throwable;

    PaymentServiceFallback(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public String pay(Integer id) {
        if(throwable instanceof FlowException) {
            // 接口限流异常
            return JSON.toJSONString(CommonResult.error("接口被限流"));
        }
        // 服务降级
        return JSON.toJSONString(CommonResult.error(throwable.getMessage()));
    }
}
